package com.java.rabbitmq.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author Woo_home
 * @create 2020/5/26 11:18
 */

public class ConnectionUtils {

    /**
     * 获取 MQ 的连接
     * @return
     */
    public static Connection getConnection() throws IOException, TimeoutException {
        // 定义一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();

        // 设置服务地址
        factory.setHost("localhost");

        // 设置端口号(AMQP)
        factory.setPort(5672);

        // 设置 virtual hosts
        factory.setVirtualHost("/vhost");

        // 设置用户名
        factory.setUsername("admin");

        // 设置密码
        factory.setPassword("admin");

        return factory.newConnection();
    }
}
